//
//  ChooseGoodAttributeAlertView.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/24.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ChooseGoodAttributeAlertView.h"

@interface ChooseGoodAttributeAlertView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <GoodAttributeOneModel *>*array;

@end

@implementation ChooseGoodAttributeAlertView

-(NSMutableArray<GoodAttributeOneModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        self.backgroundColor = HexColorA(0x000000, 0.5);
        UIButton *btn = [[UIButton alloc] initWithFrame:self.frame];
        [btn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH, ScreenW, ScreenH * 0.6 + BottomHeight)];
    self.bgView.backgroundColor = HexColor(0xffffff);
    [self addSubview:self.bgView];
    [self.bgView setBorderWithCornerRadius:12 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerTopLeft | UIRectCornerTopRight];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.bgView.nim_width - 18) * 0.5, 5, 18, 18)];
    [closeBtn setImage:[UIImage imageNamed:@"icon_shrink_close"] forState:0];
    [closeBtn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:closeBtn];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 33, self.bgView.nim_width, self.bgView.nim_height - 33 - BottomHeight)];
    self.tableView.backgroundColor = HexColor(0xffffff);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.bgView addSubview:self.tableView];
}

- (void)requestForData {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.Id.length) {
        [dic setObject:self.Id forKey:@"id"];
    }
    if (self.n_id.length) {
        [dic setObject:self.n_id forKey:@"n_id"];
    }
    [HttpRequest PostWithApi:ReceiptDeliveryGoodManagerOtherGoodAttribute params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.array = [GoodAttributeOneModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
            [self.array enumerateObjectsUsingBlock:^(GoodAttributeOneModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                obj.currentValue = obj.value.firstObject;
                [self.selectArray enumerateObjectsUsingBlock:^(GoodAttributeOneModel * _Nonnull s_obj, NSUInteger s_idx, BOOL * _Nonnull s_stop) {
                    if ([obj.name_id isEqualToString:s_obj.name_id]) {
                        obj.isSelected = YES;
                    }
                }];
            }];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseGoodAttributeAlertCell *cell = [ChooseGoodAttributeAlertCell ChooseGoodAttributeAlertCellWithTableView:tableView];
    GoodAttributeOneModel *model = self.array[indexPath.row];
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChooseGoodAttributeAlertCell *cell = [ChooseGoodAttributeAlertCell ChooseGoodAttributeAlertCellWithTableView:tableView];
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GoodAttributeOneModel *model = self.array[indexPath.row];
    model.isSelected = !model.isSelected;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    if (self.finishBlock) {
        self.finishBlock(model, model.isSelected);
    }
}

- (void)showInView:(UIView *)view {
    if (!view) {
        return;
    }
    [view addSubview:self];
    [view addSubview:self.bgView];
    CGFloat top = ScreenH - (ScreenH * 0.6 + BottomHeight);
    CGFloat height = self.bgView.nim_height;
    [self.bgView setFrame:CGRectMake(0, ScreenH, ScreenW, height)];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
        [self.bgView setFrame:CGRectMake(0, top, ScreenW, height)];
    } completion:nil];
    [self requestForData];
}

- (void)disMissView {
    CGFloat top = ScreenH - (ScreenH * 0.6 + BottomHeight);
    CGFloat height = self.bgView.nim_height;
    [self.bgView setFrame:CGRectMake(0, top, ScreenW, height)];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
        [self.bgView setFrame:CGRectMake(0, ScreenH, ScreenW, height)];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bgView removeFromSuperview];
    }];
}

@end

@interface ChooseGoodAttributeAlertCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *selectImgView;
@property (nonatomic, strong) UILabel *lineLab;

@end

@implementation ChooseGoodAttributeAlertCell

+ (instancetype)ChooseGoodAttributeAlertCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"ChooseGoodAttributeAlertCell";
    ChooseGoodAttributeAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ChooseGoodAttributeAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = HexColor(0xffffff);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 30, 50)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        [self addSubview:self.bgView];
        
        self.lineLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 49, self.bgView.nim_width, 1)];
        self.lineLab.backgroundColor = Color_backgroundColor;
        [self.bgView addSubview:self.lineLab];
        
        self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, (self.bgView.nim_height - 17) * 0.5, self.bgView.nim_width - 10 - 24 - 10 - 10, 17)];
        self.titleLab.font = BoldFontOfSize(16);
        self.titleLab.textColor = HexColor(0x2B2C44);
        [self.bgView addSubview:self.titleLab];
        
        self.selectImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 10 - 24, (self.bgView.nim_height - 24) * 0.5, 24, 24)];
        [self.bgView addSubview:self.selectImgView];
        
        self.height = self.bgView.nim_bottom;
    }
    return self;
}

-(void)setModel:(GoodAttributeOneModel *)model {
    _model = model;
    self.titleLab.text = model.name;
    self.selectImgView.image = [UIImage imageNamed:model.isSelected ? @"icon_select_y" : @"icon_select_n"];
    self.bgView.backgroundColor = model.isSelected ? HexColorA(0x3D68EB, 0.06) : HexColor(0xffffff);
}

@end
